package com.fingard.dsp.bank.directbank.cqsx01;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;

/**
 * Created by F on 2017/6/20.
 */
public class CQSX01Balance extends CQSX01Base implements IBalance {
    //单个余额查询和批量余额查询，统一采用流处理方式
    //1、循环调用Req7506BalQry.readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用RetAts7506BalQry.writeRetDetail()，写入返回ATS的明细
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {//余额查询
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
        try {
            //发送银行之前报文主体部分送到前置机的签名端口
            String bodycontent = "";
            String actNum = "";
            StringBuilder reqXml = new StringBuilder();
            reqXml.append(String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Message><Head><TrsCode>B2EActBalQry</TrsCode><ERPTrsTimestamp>%s</ERPTrsTimestamp></Head><Body><List>",
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
            while (p_req7506.readRecord()) {
                reqXml.append(String.format("<Map><AcNo>%s</AcNo></Map>",
                        p_req7506.readedDetail.actNum));
            }
            reqXml.append("</List></Body></Message>");
            //	以下2行为签名前置服务器要求送的串，qmhead无需修改
            //	构造的最终串
            String[] tmpStrRet = sendToBank(reqXml.toString(), "B2EActBalQry");

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();

                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpStatus = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    String tmpStatusText = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                    if (successRetCode.equals(tmpStatus)) {
                        while (tmpBkReader.read("/Message/Body/List/Map")) {
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AcNo");
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("Currency"));
                            tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("AvailBal");
                            tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("Balance");
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpStatusText);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }


        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
